﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.BaseService;
using DWF.Studio.BaseService.Contract;
using DWF.Studio.Custom;
using DWF.Studio.Plugin.Contract;
using DWF.Studio.Plugin.Loader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.Plugin
{
    /// <summary>
    /// 插件管理器
    /// </summary>
    internal class PluginManager
    {
        private static readonly PluginManager _pluginManager = new PluginManager();

        /// <summary>
        /// 获取插件管理器引用
        /// </summary>
        public static PluginManager Instance
        {
            get
            {
                return _pluginManager;
            }
        }

        /// <summary>
        /// 插件加载器
        /// </summary>
        private PluginLoaderBase _pluginLoader = null;

        /// <summary>
        /// 插件列表
        /// </summary>
        private List<PluginBase> _plugins = new List<PluginBase>();

        /// <summary>
        /// 插件列表
        /// </summary>
        public List<PluginBase> Plugins
        {
            get
            {
                return _plugins;
            }
        }

        private PluginManager()
        {
            _pluginLoader = new PluginMainDomainLoader();
        }

        /// <summary>
        /// 加载插件
        /// </summary>
        public void Load()
        {
            _plugins.Clear();//清理列表

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StaticResource.Plugins);//插件安装路径
            var pluginInfos = _pluginLoader.Probe(path);//探测插件信息

            if (pluginInfos != null)
            {
                PluginParameters pluginParams = new PluginParameters();//插件初始化参数
                pluginParams.AddDictionary<IBaseService>(BaseServiceManager.Instance.BaseServiceDic);

                foreach (var pluginInfo in pluginInfos)
                {
                    var plugin = _pluginLoader.Load(pluginInfo);//加载插件实例
                    plugin.Initialize(pluginParams);//初始化插件
                    _plugins.Add(plugin);
                }
            }
        }

        public async Task LoadAsync()
        {
            _plugins.Clear();//清理列表

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StaticResource.Plugins);//插件安装路径
            var pluginInfos = await _pluginLoader.ProbeAsync(path);//探测插件信息

            if (pluginInfos != null)
            {
                PluginParameters pluginParams = new PluginParameters();//插件初始化参数
                pluginParams.AddDictionary<IBaseService>(BaseServiceManager.Instance.BaseServiceDic);

                foreach (var pluginInfo in pluginInfos)
                {
                    var plugin = await _pluginLoader.LoadAsync(pluginInfo);//加载插件实例
                    plugin.Initialize(pluginParams);//初始化插件
                    _plugins.Add(plugin);
                }
            }
        }
    }
}
